﻿using Dapper;
using Estsh.Core.Base;
using Estsh.Core.Model.EnumUtil;
using Estsh.Core.Models;
using Estsh.Core.Services;
using Estsh.Core.Wms.IRepositories;
using Estsh.Core.Wms.IServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Estsh.Core.Wms.Services
{
    /// <summary>
    /// 合并台车
    /// </summary>
    public class MergeCarrierService : BaseService<BaseEntity>, IMergeCarrierService
    {
        private readonly IMergeCarrierRepository repository;
        public MergeCarrierService(IMergeCarrierRepository _repository) : base(_repository)
        {
            repository = _repository;
        }
        public SetObjectDetail GetCarrierByName(string srcRackNo)
        {

            SetObjectDetail rfInfo = new SetObjectDetail();
            //查询台车信息
            List<WmsRackPackage> wmsRacks = repository.GetCarrierByName(srcRackNo);

            if (wmsRacks.Count == 0)
            {
                //提示  [A1-2-2]:该台车未维护，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + srcRackNo + "]:该台车未维护，请检查!";
                return rfInfo;
            }
         

            rfInfo.type = "PASS";
            rfInfo.message = "[" + srcRackNo + "]:扫描成功,请扫描产品条码";
            rfInfo.wmsObjectDetail = wmsRacks.ConvertAll(s => (object)s);
            return rfInfo;
        }

        public SetObjectDetail CheckStockByCartonNo(string srcRackNo, string cartonNo, string loginId)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();
            List<SysStock> sysStocks = repository.GetSysStockBycartonNo(cartonNo);

            if (sysStocks.Count == 0)
            {
                //提示  [M0090105]:该条码不可用，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:该条码不存在，请检查!";
                return rfInfo;
            }
            else if (string.IsNullOrEmpty(sysStocks[0].PartNo))
            {
                //提示  [M0090105]:该条码是NC隔离状态，无法操作!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:该条码对应零件号为空，请检查!";
                return rfInfo;
            }

            List<WmsRackPackage> wmsRacks = repository.GetCarrierByPartNo(sysStocks[0].PartNo);
            if (wmsRacks.Count == 0)
            {
                //提示  [M0090105]:该条码不可用，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:该条码对应零件号未维护到台车，请检查!";
                return rfInfo;
            }
            List<WmsRackPackage> wmsRackPackages = repository.GetCarrierByRackAndPartNo(srcRackNo, sysStocks[0].PartNo);
            if (wmsRackPackages.Count > 0)
            {
                rfInfo.type = "PASS";
                rfInfo.message = "[" + cartonNo + "]:扫描成功,请扫描目标台车";
                rfInfo.wmsObjectDetail = wmsRackPackages.ConvertAll(s => (object)s);
                return rfInfo;
            }
            else
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + srcRackNo + "]:该台车未维护此条码对应零件，请检查!";
                return rfInfo;
            }

        }


        public SetObjectDetail CheckStockByCartonNoAndQtyAddRackNo(string srcRackNo, string cartonNo, string destRackNo, string loginId)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();
            StringBuilder stringBuilder = new StringBuilder(1024);

            List<string> sqlStrings = new List<string>();
            List<DynamicParameters> parameterList = new List<DynamicParameters>();

            List<SysStock> sysStocks = repository.GetSysStockBycartonNo(cartonNo);
            List<WmsRackPackage> wmsRackPackagesSrc = repository.GetCarrierByRackAndPartNo(srcRackNo, sysStocks[0].PartNo);
            if (wmsRackPackagesSrc.Count == 0)
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + srcRackNo + "]:该台车未维护此条码对应零件，请检查!";
                return rfInfo;
            }

            List<WmsRackPackage> wmsRackPackagesdest = repository.GetCarrierByRackAndPartNo(destRackNo, sysStocks[0].PartNo);
            if (wmsRackPackagesdest.Count > 0)
            {

                //新增料架与包装信息
                StringBuilder updateString2 = new StringBuilder(1024);
                DynamicParameters updateStringparameters2 = new DynamicParameters();
                updateString2.Append(@"UPDATE [dbo].[wms_rack_package] SET rack_num=@rack_num ,enabled='N' where ruid=@ruid");
                updateStringparameters2 = new DynamicParameters();
                updateStringparameters2.Add("@rack_num", 0);
                updateStringparameters2.Add("@ruid", wmsRackPackagesSrc[0].Ruid);

                sqlStrings.Add(updateString2.ToString());
                parameterList.Add(updateStringparameters2);

                updateString2 = new StringBuilder(1024);
                updateStringparameters2 = new DynamicParameters();
                updateString2.Append(@"UPDATE [dbo].[wms_rack_package] SET rack_num=@rack_num where ruid=@ruid");
                updateStringparameters2 = new DynamicParameters();
                updateStringparameters2.Add("@rack_num", Convert.ToInt32(wmsRackPackagesdest[0].RackNum)+Convert.ToInt32(wmsRackPackagesSrc[0].RackNum));
                updateStringparameters2.Add("@ruid", wmsRackPackagesdest[0].Ruid);

                sqlStrings.Add(updateString2.ToString());
                parameterList.Add(updateStringparameters2);

                int recNum = repository.SQLTransaction(sqlStrings, parameterList);
                if (recNum > 0)
                {
                    List<WmsRackPackage> wmsMoves = repository.GetCarrierByName(destRackNo);
                    if (wmsMoves.Count > 0)
                    {
                        rfInfo.type = "PASS";
                        rfInfo.message = "合并台车成功，请扫描源源台车";
                        rfInfo.wmsObjectDetail = wmsMoves.ConvertAll(s => (object)s);
                    }
                    else
                    {
                        rfInfo.type = "FAIL";
                        rfInfo.message = "未查询到数据，请检查！";
                    }
                }
            }
            return rfInfo;
        }
    }
}
